package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "image/png"
	"tdy-backend/app/core"
	"tdy-backend/app/member/dto"
	"tdy-backend/app/member/service"
	"tdy-backend/app/panoramic/model"
	"tdy-backend/global"
)

type MemberController struct {
}

// @Summary 注册
// @Tags 管理端-用户
// @Accept       json
// @Param	body	body	dto.RegisterDto true "注册参数"
// @Router /Member/Register [post]
func (o *MemberController) Register(c *gin.Context) {
	var err error
	var param dto.RegisterDto
	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	memberService := service.MemberService{}
	db := global.TYD_DB
	memberService.MakeORM(db)
	_, err = memberService.Register(param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "注册成功")

}

// @Summary 登录
// @Tags 管理端-用户
// @Accept       json
// @Param        body   body      dto.LoginDto  true  "登录参数"
// @Router /Member/Login [post]
func (o *MemberController) Login(c *gin.Context) {
	var err error
	var param dto.LoginDto
	if err := c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	memberService := service.MemberService{}
	memberService.MakeORM(db)
	token, err := memberService.Login(param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, map[string]string{"token": token}, "登录成功")
}

// @Summary 获取当前用户的信息
// @Tags 管理端-用户
// @Accept       json
// @Param       token	header string true "登录标识"
// @Success	1	{string}	string	"ok"
// @Failure	300001	{string}	string	"失败"
// @Router /member/getCurrentMemberInfo [get]
func (o *MemberController) GetCurrentMemberInfo(c *gin.Context) {
	token := c.Request.Header.Get("token")
	db := global.TYD_DB
	memberService := service.MemberService{}
	memberService.MakeORM(db)
	res, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, res, "登录成功")
}

// @Summary 修改密码
// @Tags 管理端-用户
// @Accept       json
// @Param       token	header string true "登录标识"
// @Param        body   body      dto.UpdatePasswordDto  true  "登录参数"
// @Router /member/updatePassword [post]
func (o *MemberController) UpdatePassword(c *gin.Context) {
	var err error
	var param dto.UpdatePasswordDto
	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	memberService := service.MemberService{}
	memberService.MakeORM(db)
	token := c.Request.Header.Get("token")
	res, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}

	_, err = memberService.UpdatePassword(res.ID, param.Password, param.OldPassword)

	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "修改成功")
}

// @Summary 测试接口
// @Tags 管理端-测试
// @Accept       json
// @Router /Member/Test [get]
func (o *MemberController) Test(c *gin.Context) {

}

// @Summary 测试接口2
// @Tags 管理端-测试
// @Accept       json
// @Router /Member/Test2 [get]
func (o MemberController) Test2(c *gin.Context) {
	//db := global.TYD_DB
	//fmt.Printf("\ntest:%p\n", db)
	//memberService := service.MemberService{}
	//memberService.MakeORM(db).Test2()

	//db := o.Base.GetDB()
	//group := model.Group{}
	//db.Find(&group)
	//fmt.Printf("test:%p", db)
	//core.Ok(c, group, "请求成功")
}

func (o *MemberController) NewTest() bool {

	db := &global.TYD_DB
	group1 := model.Group{Title: "jjjjj", WorksId: 21}
	fmt.Printf("地址2：%p", db)
	(*db).Create(&group1)
	return true
}

func (o *MemberController) Roll() bool {
	group := model.Group{Title: "啊哈哈哈啊", WorksId: 123}
	db := &global.TYD_DB
	fmt.Printf("地址3：%p", db)
	(*db).Create(&group)
	return true
}
